#include <iostream>
#include <zlib/eventsystem/I_Buffer.h>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int main()
{
	//testing IOBufferData
	IOBufferData *data = new IOBufferData;
	data->alloc(BUFFER_SIZE_INDEX_128);
	cout << data->block_size() << endl;
	data->free();
	//tesing IOBuffer for Ptr (construct and destruct)
	Ptr<IOBufferData> pdata(new_IOBufferData(BUFFER_SIZE_INDEX_128));
	cout << pdata.m_ptr->block_size() << endl;
	return 0;
}*/



/*
	IOBufferBlock的自动释放过程
	方式1.典型递归释放
		free IOBufferData1 -->free IOBufferData2 -->free IOBufferData3 -->free IOBufferData4
		free IOBufferBlock4-->free IOBufferBlock3-->free IOBufferBlock2-->free IOBufferBlock1
	方式2.半递归释放
		free IOBufferData1 -->free IOBufferData2 -->free IOBufferBlock2-->free IOBufferData3
		free IOBufferBlock3-->free IOBufferData4 -->free IOBufferBlock4-->free IOBufferBlock1

	对于第一种方式来说,如果block链表过长,可能会导致栈溢出.
	第二种方式,block1是最后释放的,其他block是顺序释放的,最长情况下,递归深度是2.所以不会导致栈溢出.*/

/*int main()
{
	//testing IOBufferBlock free
	Ptr<IOBufferBlock> pblock;
	pblock = new_IOBufferBlock(1);
	pblock.m_ptr->next = new_IOBufferBlock(2);
	pblock.m_ptr->next.m_ptr->next = new_IOBufferBlock(3);
	pblock.m_ptr->next.m_ptr->next.m_ptr->next = new_IOBufferBlock(4);
}*/
/*
int main()
{
	Ptr<IOBufferBlock> pblock;
	pblock = new_IOBufferBlock(1);
	cout << "buff size:" << pblock->block_size() << endl;
	cout << "read alive:" << pblock->read_alive() << endl;
	cout << "write alive:" << pblock->write_alive() << endl;
	memcpy(pblock->start(), "abcdef", 7);
	pblock->fill(7);
	cout << "read alive:" << pblock->read_alive() << endl;
	cout << "write alive:" << pblock->write_alive() << endl;

	Ptr<IOBufferBlock> pblock1;
	pblock1 = pblock->clone();
	char buff[1024];
	strncpy(buff,pblock1->start(), 7);
	cout << buff << endl;
	pblock1->consum(7);
	cout << "read alive:" << pblock1->read_alive() << endl;
	cout << "write alive:" << pblock1->write_alive() << endl;
	return 0;
	
}*/

int main()
{
	/*MIOBuffer mbuff;
	cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.write_alive() << endl;
	cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.write_alive() << endl;
	IOBufferBlock *block = new_IOBufferBlock(1);
	mbuff.append_block(block);
	cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.write_alive() << endl;*/

	/*MIOBuffer mbuff(3, 4096*3, true);
	cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.current_write_alive() << endl;
	cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.write_alive() << endl;
	cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.write_alive() << endl;
	cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.write_alive() << endl;*/

	/*char buff[4096];
	MIOBuffer mbuff(0, 1024, true);
	mbuff.write(buff, sizeof(buff));
	cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.current_write_alive() << endl;*/

	//MIOBuffer中的data需要IOBufferReader才能驻留.
	char buff[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddewrsdsadasdasd";
    MIOBuffer mbuff(0, 128, true);
	IOBufferReader *reader = mbuff.alloc_reader();
    mbuff.write(buff, sizeof(buff));
    cout << "empty:" <<mbuff.empty() << " readalive:" << mbuff.read_alive() << " writealive:" << mbuff.current_write_alive() << endl;    cout <<mbuff._writer->data->data()<< endl;
	cout << "read alive " << reader->read_alive() <<  "datalen " << sizeof(buff) << endl;
	char tmp[1024];
	int len = reader->read(tmp, sizeof(tmp));
	cout << "len "  << len << endl;
	write(1, tmp, len);
}
